package org.wmq.javafxmapcollectioncs.controller;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import org.wmq.javafxmapcollectioncs.God;
import org.wmq.javafxmapcollectioncs.model.Entity.MapEntity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class IndexController {

    @FXML
    private Button moveToFirstButton;

    @FXML
    private Button moveToPrevButton;

    @FXML
    private Button moveToNextButton;

    @FXML
    private Button moveToLastButton;

    @FXML
    private Button addMapButton;

    @FXML
    private Button deleteMapButton;

    @FXML
    private Button refreshButton;

    @FXML
    private Button findButton;

    @FXML
    private Button exitButton;

    @FXML
    private Button updateMapButton; // 假设有一个更新按钮

    @FXML
    private ImageView mapView;

    @FXML
    private Label nameLabel;

    @FXML
    private Label mapTypeLabel;

    @FXML
    private Label mediumLabel;

    @FXML
    private Label publicationTimeLabel;

    @FXML
    private Label collectionTimeLabel;

    @FXML
    private Label introductionLabel;

    @FXML
    private TextArea introductionText;

    @FXML
    private TextArea commentText;

    @FXML
    private TextField nameText;

    @FXML
    private TextField mapTypeText;

    @FXML
    private TextField mediumText;

    @FXML
    private DatePicker publicationTimePicker;

    @FXML
    private DatePicker collectioinTimePicker;

    @FXML
    public void initialize() throws FileNotFoundException {
        // 初始化逻辑可以放在这里
        refresh();
    }

    @FXML
    private void moveToFirst() throws FileNotFoundException {
        // 实现移动到第一个地图的逻辑
        System.out.println("Moving to first map");
        God.getGod().getMapEntityList().moveToHead();
        refresh();
    }

    @FXML
    private void moveToPrev() throws FileNotFoundException {
        // 实现移动到上一个地图的逻辑
        System.out.println("Moving to previous map");
        God.getGod().getMapEntityList().moveToPrev();
        refresh();
    }

    @FXML
    private void moveToNext() throws FileNotFoundException {
        // 实现移动到下一个地图的逻辑
        System.out.println("Moving to next map");
        God.getGod().getMapEntityList().moveToNext();
        refresh();
    }

    @FXML
    private void moveToLast() throws FileNotFoundException {
        // 实现移动到最后一个地图的逻辑
        System.out.println("Moving to last map");
        God.getGod().getMapEntityList().moveToTail();
        refresh();

    }

    @FXML
    private void addMap() throws IOException {
        // 实现添加地图的逻辑
        System.out.println("Adding a new map");
        Pane pane = FXMLLoader.load(getClass().getResource("/org/wmq/javafxmapcollectionstandalone/add-view.fxml"));
        Stage addStage = new Stage();
        addStage.setScene(new Scene(pane));
        addStage.showAndWait();
        refresh();
    }

    @FXML
    private void deleteMap() throws FileNotFoundException {
        // 实现删除地图的逻辑
        System.out.println("Deleting the current map");

        God.getGod().getMapEntityService().deleteById(God.getGod().getMapEntityList().getCurrentData().getId());
        God.getGod().getCommentService().deleteById(God.getGod().getMapEntityList().getCurrentData().getId());
        God.getGod().getMapEntityList().removeCurrentData();
        refresh();
    }

    @FXML
    private void refresh() throws FileNotFoundException {
        // 实现刷新地图的逻辑
        System.out.println("Refreshing the map view");
        MapEntity mapEntity = God.getGod().getMapEntityList().getCurrentData();
        if (mapEntity == null){
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("提示");
            alert.setContentText("当前无地图数据，请添加");
            alert.showAndWait();
            return;
        }
        FileInputStream fileInputStream = new FileInputStream(new File(mapEntity.getUrl()));
        mapView.setImage(new Image(fileInputStream));
        nameText.setText(mapEntity.getName());
        mapTypeText.setText(mapEntity.getMapType());
        mediumText.setText(mapEntity.getMedium());
        publicationTimePicker.setValue(mapEntity.getPublicationTime());
        collectioinTimePicker.setValue(mapEntity.getCollectionTime());
        introductionText.setText(mapEntity.getIntroduction());
        commentText.setText(God.getGod().getCommentService().getById(mapEntity.getId()));
    }

    @FXML
    private void exit() {
        // 实现退出应用的逻辑
        System.out.println("Exiting the application");
        System.exit(0);
    }

    @FXML
    private void updateMap() throws Exception {
        // 实现更新地图的逻辑
        System.out.println("Updating the map");
        Pane pane = FXMLLoader.load(getClass().getResource("/org/wmq/javafxmapcollectionstandalone/update-view.fxml"));
        Stage updateStage = new Stage();
        updateStage.setScene(new Scene(pane));
        updateStage.showAndWait();
        refresh();
    }

    @FXML
    public void findMap() throws IOException {
        System.out.println("Finding map");
        Stage stage = new Stage();
        Pane pane = FXMLLoader.load(getClass().getResource("/org/wmq/javafxmapcollectionstandalone/find-view.fxml"));
        stage.setScene(new Scene(pane));
        stage.showAndWait();
        refresh();
    }

    @FXML
    public void comment() throws IOException {
        System.out.println("comment");
        God.getGod().getCommentService().add(God.getGod().getMapEntityList().getCurrentData().getId(),commentText.getText());
    }
}